In [1]:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from gtts import gTTS
import matplotlib
import numpy
from traitlets.config.manager import BaseJSONConfigManager
path = "/Users/Kristen/anaconda3/envs/py36/etc/jupyter/nbconfig"
cm = BaseJSONConfigManager(config_dir=path)
cm.update("livereveal", {"autolaunch": True,
"theme": "sky",
}
)
#Supress default INFO logging
# The UT Dallas Art Science Lab Training module
print ("Sonification Training Module v0.5.2")
print("\nDeveloped by the ArtSciLab at UT Dallas")
print("\n")
print("Panned Pulse Testing Module" )
print("\n")
print("\nBasic Instructions for each cell :")
print('1. Press Shift + Enter to initiate display')
print("2. Press Space Bar to proceed to the next cell")
print("\n")
print("Volume controls are located in the top row of the keyboard if you need to adjust the volume at any time")
In [1]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import Button, HBox, VBox,Layout
from ipywidgets import widgets
from ipywidgets import interact, interactive, fixed, interact_manual
from gtts import gTTS
import os
import numpy
import ctcsound
import platform
import os
speechflag = 0
if (platform.system()=='Windows'):
speechflag = 2
if (platform.system()!='Windows'):
speechflag = 1
print("Press Shift + Enter to activate cell")
print ("\nListen to the Sonification, then set the Slider to what value you think it represents.")
print("Pay close attention to the pulse rate and panning (left/right).")
print("You can listen to the sonification as many times as you need to.")
print("\nWhen you are sure of your answer, click Submit Response once and wait for the next trial to load.")
print("You will undergo 5 trials.")
count=0
accuracy =0
pan = 0
user_input=0
cs = ctcsound.Csound()
index = 0
csd = '''
<CsoundSynthesizer>
<CsOptions>
-odac -d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
;aMod1 poscil 200, 700, 1
aMod1 poscil p4, p5, 1 ; p4 = amp1, p5 = f1, p6 = amp2, p7 = f2
;aMod2 poscil 1800, 290, 1
aMod2 poscil p6, p7, 1
kenv linen p9 , 0.3 , p3, p9
aSig poscil kenv, 440+aMod1+aMod2, 1
outs aSig*(1-p8), aSig*p8
endin
</CsInstruments>
<CsScore>
f 0 14400
f 1 0 1024 10 1
</CsScore>
</CsoundSynthesizer>
'''
try:
pt
except NameError:
var_exists = False
else:
pt.stop()
pt.join()
time.sleep(2)
cs.compileCsdText(csd)
cs.start()
pt = ctcsound.CsoundPerformanceThread(cs.csound())
pt.play()
def f(percentage):
global user_input
user_input = percentage
def redraw():
global index
global accuracy
sonibutton = widgets.Button(description = 'Listen to Sonification')
answerbutton = widgets.Button(description='Submit Response')
choices = random.sample(range(100), 4)
choices = list(map(str, choices))
correct = random.choice(choices)
index = int(correct)
#display(Image(correct))
#display(correct)
time.sleep(0.5)
#display(button)
#button.on_click(on_button_clicked)
#buttons = [widgets.Button(description = choice) for choice in choices]
#sonibutton = [widgets.Button(description = 'Listen to Sonification')]
interact (f, percentage=(0,100,1))
#answerbutton = [widgets.Button(description='Submit Input')]
#container = widgets.HBox(children=buttons)
left_box = VBox([(sonibutton)])
right_box = VBox([(answerbutton)])
#HBox([left_box, right_box])
container = widgets.HBox([left_box,right_box])
print("Trial " + str(count+1))
display(container)
def ans_button_clicked(b):
global count
global accuracy
count = count + 1
tts = gTTS(text='Input Submitted', lang='en')
tts.save("answer.mp3")
if (speechflag==1):
os.system("afplay answer.mp3")
if (speechflag==2):
os.system("cmdmp3 answer.mp3")
#print(user_input)
text=list()
text.append(index)
text.append(user_input)
text.append(index-user_input)
accuracy = accuracy + abs(index -user_input)
with open('responses.csv','a') as file:
file.write('\n')
for line in text:
file.write(str(line))
file.write(',')
time.sleep(2)
container.close()
clear_output()
if count <5:
redraw()
if count == 5:
msg = widgets.Button(description = 'Thank you for finishing this module',layout=Layout(width='50%', height='80px'))
display(msg)
print("Your accuracy of response is " + str(100-(accuracy/5)) + "%")
pt.stop()
pt.join()
file.close()
def son_button_clicked(b):
#tts = gTTS(text='Playing Sonification', lang='en')
#tts.save("answer.mp3")
#os.system("afplay answer.mp3")
in_min = 0
in_max = 100
out_min=690
out_max = 710
global index
if (index>50):
pan = 1
if (index<50):
pan = 0
if (index==50):
pan = 0.5
freq = (index - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
#print(freq)
pt.scoreEvent(False, 'i', (1, 0, 4, 200, 700, 200, freq, pan, 0.5))
time.sleep(4.5)
answerbutton.on_click(ans_button_clicked)
sonibutton.on_click(son_button_clicked)
redraw()
Now that you have been evaluated on this sound representation, it is time to move on to the next set where you will learn a new sonification.
Please close this tab and open the next module from the list of notebooks:
8. Plucked String Time Series Training Module